package com.tongtech.system.controller;

import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.core.web.page.TableDataInfo;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.WorkOrder;
import com.tongtech.system.service.IWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 工单信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/workOrder")
public class WorkOrderController extends BaseController
{
    @Autowired
    private IWorkOrderService workOrderService;

    /**
     * 获取工单列表
     */
    @RequiresPermissions("workOrder:info:list")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrder workOrder)
    {
        startPage();
        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        return getDataTable(list);
    }

    /**
     * 根据工单编号获取详细信息
     */
    @RequiresPermissions("workOrder:info:query")
    @GetMapping(value = "/{workOrderId}")
    public AjaxResult getInfo(@PathVariable Long workOrderId)
    {
        return success(workOrderService.selectWorkOrderById(workOrderId));
    }

    /**
     * 获取员工周期内的任务
     */
    @GetMapping(value = "/plans")
    public AjaxResult plans(@RequestParam("startDate") String startDate, @RequestParam("endDate") String endDate)
    {
        return success(workOrderService.selectPlans(startDate, endDate));
    }

    /**
     * 新增工单
     */
    @RequiresPermissions("workOrder:info:add")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WorkOrder workOrder)
    {
       if(Objects.nonNull(workOrder)){
           workOrder.setCreateBy(SecurityUtils.getUsername());
       }
        return toAjax(workOrderService.insertWorkOrder(workOrder));
    }

    /**
     * 修改工单
     */
    @RequiresPermissions("workOrder:info:edit")
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WorkOrder workOrder)
    {
        if(Objects.nonNull(workOrder)){
            workOrder.setUpdateBy(SecurityUtils.getUsername());
        }
        return toAjax(workOrderService.updateWorkOrder(workOrder));
    }

    /**
     * 删除工单
     */
    @RequiresPermissions("workOrder:info:remove")
    @Log(title = "工单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workOrderId}")
    public AjaxResult remove(@PathVariable Long workOrderId)
    {
        return toAjax(workOrderService.deleteWorkOrderById(workOrderId));
    }
}
